/**
 * 
 */
package org.hotpotmaterial.codegenn2.config.security.service.impl;

import java.util.UUID;

import org.hotpotmaterial.codegenn2.common.HmCodeGenConstants;
import org.hotpotmaterial.codegenn2.config.security.service.IAccountTokenService;
import org.hotpotmaterial.codegenn2.entity.HmteamAccount;
import org.springframework.beans.factory.annotation.Autowired;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;

/**
 * @author cakydeveloper
 *
 */
public class AccountTokenServiceImpl implements IAccountTokenService {
	
	@Autowired
	private CacheChannel cacheChannel; 

	@Override
	public HmteamAccount getUser(String token) {
		CacheObject cacheobj = cacheChannel.get(HmCodeGenConstants.CACHE_REGION_TOKEN_KEY, token, false);
		return (HmteamAccount) cacheobj.getValue();
	}

	@Override
	public String createToken(HmteamAccount user) {
		String token = UUID.randomUUID().toString();
		cacheChannel.set(HmCodeGenConstants.CACHE_REGION_TOKEN_KEY, token, user);
		return token;
	}

	@Override
	public void deleteToken(String token) {
		cacheChannel.evict(HmCodeGenConstants.CACHE_REGION_TOKEN_KEY, token);
	}

}
